package com.wonders.frame.controller;

import com.gcr.server.response.Response;
import com.wonders.frame.common.Commons;
import com.wonders.frame.common.Enum.InitCommonSourceEnum;
import com.wonders.frame.common.Enum.ResponseCodeEnum;
import com.wonders.frame.controller.core.CoreController;
import com.wonders.frame.entity.AuthDic;
import com.wonders.frame.entity.form.AuthDicForm;
import com.wonders.frame.service.AuthDicService;
import com.wonders.frame.service.Core.CoreCurdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/authDic")
public class AuthDicController extends CoreController<AuthDic> {
    Logger log = LoggerFactory.getLogger(AuthDicController.class);

    @Autowired
    private AuthDicService authDicService;
    @Override
    protected CoreCurdService getService() {
        return authDicService;
    }

    private final ReentrantLock lock=new ReentrantLock();

    //获取单个字典树形结构
    @RequestMapping("getAuthDicByDicType")
    public Response getAuthDicByDicType(String dicType){
        try {
            return successResponse(authDicService.getAuthDicByDicType(dicType));
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }
    }

    @RequestMapping("getAuthDicListByDicType")
    public Response getAuthDicListByDicType(String dicType){
        try {
            return successResponse(authDicService.getAuthDicListByDicType(dicType));
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }

    }

    /**
     * 删除字典
     */
    @RequestMapping("deleteAuthDicById")
    public Response deleteAuthDicById(String id){
        Response res = new Response();
        try {
            authDicService.deleteById(id);
            InitCommonSourceEnum.INSTANCE.initDic();
            res.setCode(ResponseCodeEnum.SUCCESS.getCode());
            res.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();

        }
    }

    /**
     * 新增字典
     *
     */
    @RequestMapping("saveAuthDic")
    public Response saveAuthDic(@RequestBody AuthDicForm authDicForm){
        String id = authDicForm.getId();
        String code = authDicForm.getCode();
        String name = authDicForm.getName();
        String dicType = authDicForm.getDicType();
        try {
            lock.lock();
            log.info("authDicType/saveAuthDic");
            Integer checkCode = authDicService.checkUnique(id, code, name, dicType);
            if (checkCode == 0) {
                AuthDic authDic = authDicService.saveAuthDic(authDicForm);
                InitCommonSourceEnum.INSTANCE.initDic();
                return successResponse(authDic);
            } else if (checkCode == 1) {
                return errorResponse(ResponseCodeEnum.DIC_CODE_REPEAT, null);
            } else if (checkCode == 2) {
                return errorResponse(ResponseCodeEnum.DIC_NAME_REPEAT, null);
            } else {
                return errorResponse();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }finally {
            lock.unlock();
        }

    }

    /**
     * 从内存中获取字典值
     */
    @RequestMapping("getAuthDicMap")
    public Response getAuthDicMap(String dicType){
        List<Map<String, String>> listMap = new ArrayList<>();
        Map<String, String> map = Commons.returnDicMap(dicType);
        for (String key : map.keySet()) {
            Map<String, String> m = new HashMap<>();
            m.put("key",key);
            m.put("value",map.get(key));
            listMap.add(m);
        }
        return successResponse(listMap);
    }

    //查询全部字典值，全部导出
    @RequestMapping("/getAllDicValues")
    public Response getAllDicValues(){
        try {
            return successResponse(authDicService.getAllDicValues());
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }
    }

}
